package rpc
{
	import events.HTTPCommanderEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.setTimeout;
	
	import mx.core.ClassFactory;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.http.SerializationFilter;
	import mx.utils.ObjectUtil;
	
	[Event(name="HTTPCommanderResultComplete", type="events.HTTPCommanderEvent")]
	
	public class HTTPCommander extends EventDispatcher implements IHTTPCommander
	{
		private var _service:HTTPService;
		private var _url:String = "";
		private var _resultFormat:String = "object";
		private var _autoSend:Boolean = false;
		private var _params:Object;
		private var _delegate:Class;
		private var _classFactory:Object;
		private var _resultData:Object;
		
		public function HTTPCommander(target:IEventDispatcher=null)
		{
			super(target);
			_service = new HTTPService();
			_params = new Object();
			_service.addEventListener(ResultEvent.RESULT,resultHandler,false,0,true);
			_service.addEventListener(FaultEvent.FAULT,faultHandler,false,0,true);
			addEventListener(HTTPCommanderEvent.HTTPCOMMANDER_DELEGATE_COMPLETE,delegateHandler,false,0,true);
			setTimeout(autoSendService,0);
		}
		
		public function send():void
		{
			if(_delegate){
				_classFactory = new ClassFactory(_delegate).newInstance();
			}
			_service.url = _url;
			_service.resultFormat = _resultFormat;
			_service.send(_params);
		}
		
		private function autoSendService():void
		{
			if(_autoSend){
				send();
			}
		}
		
		private function resultHandler(event:ResultEvent):void
		{
			if(_classFactory){
				
				//_classFactory.dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_DELEGATE,event.result,false,true));
				
				if(_classFactory is IHTTPCommanderDelegate){
					_classFactory.target = this;
					_classFactory.dataProvider = ObjectUtil.copy(event.result);
					_classFactory.dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_DELEGATE,null,false,true));
					
				}else{
					resultData = event.result;
					dispatchEvent(event);
					dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_RESULT_COMPLETE,resultData,false,true));
				}
				
			}else{
				//_classFactory.target = sel
				resultData = event.result;
				dispatchEvent(event);
				dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_RESULT_COMPLETE,resultData,false,true));
				
			}
		}
		private function faultHandler(event:FaultEvent):void
		{
			trace(ObjectUtil.toString(event));
			dispatchEvent(event);
		}
		private function delegateHandler(event:HTTPCommanderEvent):void
		{
			resultData = event.eventData;
			dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_RESULT_COMPLETE,resultData,false,true));
			
		}
		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			_url = value;
		}

		public function get autoSend():Boolean
		{
			return _autoSend;
		}

		public function set autoSend(value:Boolean):void
		{
			_autoSend = value;
		}
		
		public function get params():Object
		{
			return _params;
		}
		
		public function set params(value:Object):void
		{
			_params = value;
		}
		
		public function get delegate():Class
		{
			return _delegate;
		}

		public function set delegate(value:Class):void
		{
			_delegate = value;
		}	
		
		[Inspectable(enumeration="object,array,xml,flashvars,text,e4x", category="General")]
		public function get resultFormat():String
		{
			return _resultFormat;
		}
		
		public function set resultFormat(value:String):void
		{
			_resultFormat = value;
		}
		
		public function get resultData():Object
		{
			return _resultData;
		}

		public function set resultData(value:Object):void
		{
			_resultData = value;
		}


	}
}